<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <date-range-picker
          v-model="query.createTime"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="date-item"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" append-to-body :title="crud.status.title" width="850px">
        <Cal ref="cal" class="cal" />

        <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="65px">

          <el-form-item label="银行卡">
            <el-input v-model="form.card" style="width: 180px;" maxlength="20" />
          </el-form-item>
          <el-form-item label="微信">
            <el-input v-model="form.wx" style="width: 180px;" maxlength="20" />
          </el-form-item>
          <el-form-item label="支付宝">
            <el-input v-model="form.pay" style="width: 180px;" />
          </el-form-item>
          <el-form-item label="借">
            <el-input v-model="form.lend" style="width: 270px;" @keyup.native.enter="sumA()" />
          </el-form-item>
          <el-form-item label="证券">
            <template>
              <el-input v-model="form.bond" style="width: 270px;" />
            </template>
          </el-form-item>
          <el-form-item label="详情" prop="detail">
            <Editor ref="editor" class="eladmin-editor" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <Editor ref="remarkEditor" class="eladmin-editor" />

            <!-- <el-input v-model="form.remark" type="textarea" :rows="3" style="width: 600px;border:1px solid grey" /> -->
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>

      <el-dialog
        :before-close="summaryClose"
        :title="titleData"
        :visible.sync="handleState"
        width="700px"
        center
      > <div v-html="allContent" />
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column align="center" prop="remark" type="expand">
          <template slot-scope="scope">
            <div v-if="scope.row.remark===undefined">暂无信息</div>
            <div v-else v-html="scope.row.remark" />
          </template>
        </el-table-column>
        <el-table-column label="查看" align="center">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-search" @click="getContent(scope.row)" />
          </template>
        </el-table-column>
        <el-table-column prop="card" label="银行卡" align="center" />
        <el-table-column prop="wx" label="微信" align="center" />
        <el-table-column prop="pay" label="支付宝" align="center" />
        <el-table-column prop="lend" label="借" align="center" />
        <el-table-column prop="bond" label="证券" align="center" />
        <el-table-column label="总合" align="center">
          <template slot-scope="scope">
            <el-tag class="sum" type="danger" style="font-size:13px">{{ getSum(scope.row)+'元' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="完成率" align="center">
          <template slot-scope="scope">
            <el-progress :text-inside="true" :stroke-width="18" :percentage="getFinishRate(scope.row)" :color="customColorMethod(getFinishRate(scope.row))" />
          </template>
        </el-table-column>
        <!-- <el-table-column prop="detail" label="详情" /> -->
        <el-table-column prop="createTime" label="创建时间" align="center" />
        <el-table-column v-if="checkPer(['admin','money:edit','money:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
      <lineChart ref="lineChart" />
    </div>
  </div>
</template>

<script>
import lineChart from './lineChart.vue'
import crudMoney from '@/api/money'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import Editor from '@/views/components/MyEditor'
import Cal from '@/views/property/money/cal'
import DateRangePicker from '@/components/DateRangePicker'

const defaultForm = { remark: null, id: null, createTime: null, card: null, wx: null, pay: null, lend: null, bond: null, detail: '<table border="1" width="100%" cellpadding="0" cellspacing="0"><tbody><tr><td>&nbsp;银行卡</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>&nbsp;微信</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>支付宝&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>借&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>证券</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></tbody></table><p><br></p>' }
export default {
  name: 'Money',
  components: { lineChart, pagination, crudOperation, rrOperation, udOperation, Editor, DateRangePicker, Cal },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'money', url: 'api/money', idField: 'id', sort: 'createTime,desc', crudMethod: { ...crudMoney }})
  },
  data() {
    return {
      titleData: '详情展示',
      allContent: null,
      handleState: false,
      calState: false,
      permission: {
        add: ['admin', 'money:add'],
        edit: ['admin', 'money:edit'],
        del: ['admin', 'money:del']
      },
      rules: {
      },
      queryTypeOptions: [
      ]
    }
  },
  mounted() {
    crudMoney.getLineChart().then(res => {
      console.log(res)
      this.$refs.lineChart.data = res
      this.$refs.lineChart.initChart()
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    /** 开始 "新建/编辑" - 之后 */
    [CRUD.HOOK.afterToCU](crud, form) {
      var that = this
      setTimeout(function() {
        // 初始化富文本编辑器
        that.$refs.editor.set(that.form.detail)
        that.$refs.remarkEditor.set(that.form.remark)
        // 初始化文件列表
        // that.$refs.fileUpload.init(that.form.filePath, that.form.fileName)
      }, 0)
    },
    [CRUD.HOOK.beforeValidateCU]() {
      // 提交前获取富文本编辑器内数据
      this.crud.refresh()
      this.form.detail = this.$refs.editor.get()
      this.form.remark = this.$refs.remarkEditor.get()

      // // 提交前获取文件列表路径与名称
      // if (this.$refs.fileUpload.upLoadForm.fileName) {
      //   this.form.fileName = this.$refs.fileUpload.upLoadForm.fileName
      //   this.form.filePath = this.$refs.fileUpload.upLoadForm.filePath
      // } else {
      //   this.form.fileName = ''
      //   this.form.filePath = ''
      // }
    },
    getSum(data) {
      var sum = parseInt(data.card) +
      parseInt(data.bond) + parseInt(data.wx) +
      parseInt(data.lend) + parseInt(data.pay)
      return sum
    },
    getContent(data) {
      console.log(data.detail)
      this.allContent = data.detail
      this.handleState = true
    },
    summaryClose() {
      console.log('---')
      this.handleState = false
    },

    getFinishRate(data) {
      return Number(((this.getSum(data) / 200000) * 100).toFixed(1))
    },
    customColorMethod(percentage) {
      if (percentage < 30) {
        return '#909399'
      } else if (percentage < 70) {
        return '#e6a23c'
      } else {
        return '#67c23a'
      }
    }

  }
}
</script>

<style scoped>
.cal{
  position: fixed;
  right: 28px;
  top: 150px;
  z-index: 999999;

}
</style>
